
clear all;
close all;

TFI = { 3, 12, 4, 16, 8 };

TF = GSLpp_testfunctions();
TF{TFI{1}}.n = 8;
TF{TFI{3}}.n = 8;

A = { "gsl_ext_bfgs_mt", "gsl_ext_conjgrad_fr_mt", "gsl_ext_conjgrad_pr_mt" };

ETA_VALUES = linspace(0.005, 0.8, 100);

PLOT_COLORS = { "r", "g", "b" };

A_titles = cell(length(A), 1);
for ai = 1:length(A)
  A_titles{ai} = strrep(A{ai}, "_", '\_');
endfor

for tfi = 1:length(TFI)
  tf = TF{TFI{tfi}};
  tf.stopcrit_params.eps = 1e-8;

  if isfield(tf, "n")
    n = tf.n;
  else
    n = 0;
  endif

  x0 = GSLpp_get_startingpoint(tf.starting_point, n);

  eta_idx = 1;
  for param_eta = ETA_VALUES
    lsp = struct("tol", param_eta);

    for ai = 1:length(A)
      results = GSLpp_minimize(A{ai}, struct, lsp, tf.name, struct("n", n), ...
                              struct("f", "sym", "g", "sym", "H", "sym"), ...
                              "xdisttomin", tf.stopcrit_params, x0,
                              0, false);

      num_iter(ai, eta_idx) = results.numiter;
      num_func_eval(ai, eta_idx) = results.nfeval;
      num_grad_eval(ai, eta_idx) = results.ngeval;
    endfor

    eta_idx = eta_idx + 1;
  endfor

  GSLpp_newplot();
  hold on;
  for ai = 1:length(A)
    plot(ETA_VALUES, num_iter(ai, :), PLOT_COLORS{ai}, "linewidth", 6);
  endfor
  hold off;

  axis([ ETA_VALUES(1), ETA_VALUES(length(ETA_VALUES)), 0, max(max(num_iter) * 1.3) ]);
  xlabel('Line search parameter \eta');
  ylabel("Number of iterations");
  legend(A_titles, "location", "northwest");
  legend("boxon");
  title(tf.title);

  print([ "linminparams_test_", tf.name, "_numiter.ps" ], "-color", "-dashed", "-landscape", "-FArial:22");
  close;

  GSLpp_newplot();
  hold on;
  for ai = 1:length(A)
    plot(ETA_VALUES, num_func_eval(ai, :), PLOT_COLORS{ai}, "linewidth", 6);
  endfor
  hold off;

  axis([ ETA_VALUES(1), ETA_VALUES(length(ETA_VALUES)), 0, max(max(num_func_eval) * 1.3) ]);
  xlabel('Line search parameter \eta');
  ylabel("Number of function and gradient evaluations");
  legend(A_titles, "location", "northwest");
  legend("boxon");
  title(tf.title);

  print([ "linminparams_test_", tf.name, "_numfeval.ps" ], "-color", "-dashed", "-landscape", "-FArial:22");
  close;
endfor
